//
//  AnchorInfoCell.swift
//  FM
//
//  Created by x on 2017/9/26.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class AnchorInfoCell: UITableViewCell {
    
    /// 主播数据模型
    var model:HomeFM? {
        didSet {
            //设置内容
            if model?.diantai != nil {
                anchorImage.sd_setImage(with: model?.diantai?.user?.avaterURL)
                contentLable.text = model?.diantai?.content
                likeLable.text = "喜欢:"+(model?.diantai?.favnum)!
                collectionLable.text = "收藏:"+(model?.diantai?.collectionNum)!
            }else {
                anchorImage.sd_setImage(with: model?.iconImageURL)
                contentLable.text = model?.speak
                likeLable.text = "喜欢:"+(model?.favnum)!
                collectionLable.text = "收藏:"+(model?.collectionNum)!
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    func setupUI() -> Void {
        
        backgroundColor = UIColor.clear
        
        //1.添加子控件
        contentView.addSubview(headbgLable)
        contentView.addSubview(headtitleLable)
        contentView.addSubview(anchorImage)
        contentView.addSubview(contentLable)
        contentView.addSubview(likeLable)
        contentView.addSubview(collectionLable)
        contentView.addSubview(footbgLable)
        contentView.addSubview(foottitleLable)
        contentView.addSubview(lineLbale)
        
        //2.布局子控件
        let dic = ["headbgLable":headbgLable,
                   "headtitleLable":headtitleLable,
                   "anchorImage":anchorImage,
                   "contentLable":contentLable,
                   "likeLable":likeLable,
                   "collectionLable":collectionLable,
                   "footbgLable":footbgLable,
                   "foottitleLable":foottitleLable,
                   "lineLbale":lineLbale] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[headbgLable(3)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[headbgLable(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[headbgLable]-7-[headtitleLable]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[anchorImage(80)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[headbgLable]-10-[anchorImage(80)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[anchorImage]-4-[contentLable]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[likeLable]-10-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[likeLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[collectionLable]-10-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[likeLable]-20-[collectionLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[footbgLable(3)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[anchorImage]-20-[footbgLable(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[footbgLable]-7-[foottitleLable]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)

        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[lineLbale]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[footbgLable]-5-[lineLbale(0.5)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        contentView.addConstraints(cons)
    }
    
       
    //MARK: -- 懒加载
    
    private lazy var headbgLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.red)
        
    private lazy var headtitleLable:UILabel = UILabel.lableWith(title: "主播介绍", titleColor: UIColor.black, fontSize: 12)
    
    private lazy var anchorImage:UIImageView = UIImageView .imageWith(radios: 40, imageName: "")
    
    private lazy var contentLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.gray, fontSize: 10)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        tt.preferredMaxLayoutWidth = self.bounds.width-160;
        
        return tt
    }()
    
    private lazy var likeLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
    
    private lazy var collectionLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
    
    private lazy var footbgLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.yellow)
    
    private lazy var foottitleLable:UILabel = UILabel.lableWith(title: "最新评论", titleColor: UIColor.black, fontSize: 12)
    
    private lazy var lineLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor(white: 0.9, alpha: 1.0))
        
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
